import checkTypes from 'check-types';
import sortArray from 'sort-array';
import deepEqual from 'deep-equal';
import getValue from 'get-value';
import setValue from 'set-value';
import deepmerge from 'deepmerge';
import deepKeys from 'deep-keys';

//深度合并JSON对象（不合并数组值）
export default (obj, params) => {
  if (!checkTypes.object(obj)) {
    return;
  }
  if (!checkTypes.object(params)) {
    return;
  }

  for (const deepKey of deepKeys(params)) {
    const oldVal = getValue(obj, deepKey);
    const parVal = getValue(params, deepKey);
    let newVal = parVal;
    if (checkTypes.object(parVal)) {
      newVal = deepmerge(oldVal, parVal);
    }
    setValue(obj, deepKey, newVal);
  }
};
